﻿using System;
using System.Collections.Generic;

namespace DL.Profile
{
    public class ProfileManager
    {
        private static Dictionary<Type, IProfile> _profiles = new();

        public static T Get<T>() where T : class, IProfile
        {
            var typ = typeof(T);
            IProfile result;
            var hasVal = _profiles.TryGetValue(typ, out result);
            if (!hasVal || result == null)
            {
                result = InstanceProfile<T>();
                _profiles[typ] = result;
            }

            return (T)result;
        }

        private static T InstanceProfile<T>()
        {
            return Activator.CreateInstance<T>();
        }
        
        public static void Unload<T>() where T : class, IProfile
        {
            var typ = typeof(T);
            if (_profiles.ContainsKey(typ))
            {
                _profiles.Remove(typ);
            }
        }

        public static void UnloadAll()
        {
            // var typ = typeof(UserProfile);
            // IProfile result;
            // var hasVal = _profiles.TryGetValue(typ, out result);
            // if (hasVal && result != null)
            // {
            //     result = null;
            // }
            //
            // _profiles.Clear();
        }

        public static void Reset()
        {
            UnloadAll();
        }


    }
}